import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/HomeView.vue')
    },
    {
      path: '/docs',
      name: 'docs',
      component: () => import('@/views/DocsView.vue')
    },
    {
      path: '/components',
      name: 'components',
      component: () => import('@/views/ComponentsView.vue')
    },
    {
      path: '/themes',
      name: 'themes',
      component: () => import('@/views/ThemesView.vue')
    },
    {
      path: '/resources',
      name: 'resources',
      component: () => import('@/views/ResourcesView.vue')
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('@/views/AboutView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegisterView.vue')
    }
  ]
})

// 全局路由守卫（建议放在路由配置文件中）
router.beforeEach((to, from, next) => {
  const publicPages = ['/', '/login', '/register']
  const authRequired = !publicPages.includes(to.path)
  const loggedIn = localStorage.getItem('token')

  // 需要登录且未登录时跳转首页
  if (authRequired && !loggedIn) {
    next('/')
  } else {
    next()
  }
})

export default router
